//
//  signin.m
//  testvideo
//
//  Created by fcbayern on 2021/1/4.
//  Copyright © 2021 fcbayern. All rights reserved.
//

#import "signin.h"
#import "Masonry.h"
#import "signup.h"
#import "AFNetworking.h"
#import "testtable.h"
#import "AppDelegate.h"
#import "setting.h"
#import "releaseViewController.h"
#import <objc/runtime.h>

@interface signin ()
@property(strong,nonatomic)UITextField * username;
@property(strong,nonatomic)UITextField * password;
@property(strong,nonatomic)UIButton * signin;
@property(strong,nonatomic)UIButton * signup;
@property(strong,nonatomic)UILabel * logotitle;
@property(strong,nonatomic)UIImageView * logo;

@end

@implementation signin

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.title = @"登录";
    _username = [[UITextField alloc]init];
    _username.placeholder = @"请输入邮箱";

    Ivar ivar =  class_getInstanceVariable([UITextField class], "_placeholderLabel");
    UILabel *placeholderLabel = object_getIvar(_username, ivar);
    placeholderLabel.textColor = [UIColor whiteColor];
    
    _username.layer.borderWidth = 1.0f;
    _password = [[UITextField alloc]init];
    _password.placeholder = @"请输入密码";
    
    Ivar ivar2 =  class_getInstanceVariable([UITextField class], "_placeholderLabel");
    UILabel *placeholderLabel2 = object_getIvar(_password, ivar2);
    placeholderLabel2.textColor = [UIColor whiteColor];
    _password.layer.borderWidth = 1.0f;
    _password.secureTextEntry = YES;
    _password.layer.cornerRadius = 15;
    _username.layer.cornerRadius = 15;
    _logo = [[UIImageView alloc] init];
    _logotitle = [[UILabel alloc]init];
    [self.view addSubview:_logo];
    [self.view addSubview:_logotitle];
    [self.view addSubview:_username];
    [self.view addSubview:_password];
    _password.textColor = [UIColor whiteColor];
    _username.textColor = [UIColor whiteColor];
    _logotitle.text = @"拥抱\n每一种生活";
    _logotitle.numberOfLines = 0;
    _logotitle.font = [UIFont fontWithName:@"PingFang SC" size: 30];
    _logotitle.textColor=[UIColor colorWithRed:192.0/255.0 green:192.0/255.0 blue:192.0/255.0 alpha:1];
    UIImage * logoimage =[UIImage imageNamed:@"toktik"];
    _logo.image = logoimage;
    [_logo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(100);
        make.centerX.equalTo(self.view.mas_centerX).with.offset(-100);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.2);
        make.height.equalTo(self.view.mas_width).multipliedBy(0.2);
    }];
    [_logotitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(70);
        make.left.equalTo(_logo).with.offset(120);
        make.height.equalTo(_logo).multipliedBy(2);
        make.width.equalTo(self.view.mas_width);
    }];
    
    
    
    [_username mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(_logotitle.mas_bottom).offset(self.view.frame.size.height * 0.05);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.7);
        make.height.equalTo(self.view.mas_height).multipliedBy(0.08);
    }];
    
    [_password mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(_username.mas_top).offset(self.view.frame.size.height * 0.1);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.7);
        make.height.equalTo(self.view.mas_height).multipliedBy(0.08);
    }];
    
    _signup = [[UIButton alloc]init];
    _signin = [[UIButton alloc]init];
    [_signin setTitle:@"登录" forState:UIControlStateNormal];
    [_signin setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_signup setTitle:@"注册" forState:UIControlStateNormal];
    [_signup setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _signin.backgroundColor = [UIColor colorWithRed:228/255.0 green:40/255.0 blue:74/255.0 alpha:1];
    _signup.backgroundColor = [UIColor colorWithRed:228/255.0 green:40/255.0 blue:74/255.0 alpha:1];
    _signin.layer.cornerRadius = 15;
    _signup.layer.cornerRadius = 15;
    [self.view addSubview:_signin];
    [self.view addSubview:_signup];
    [_signin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(_password.mas_bottom).offset(self.view.frame.size.height * 0.04);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.8);
        make.height.equalTo(self.view.mas_width).multipliedBy(0.2);
    }];

    [_signup mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(_signin.mas_bottom).offset(self.view.frame.size.height * 0.05);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.8);
        make.height.equalTo(self.view.mas_width).multipliedBy(0.2);
    }];
    
    
    [_signin addTarget:self action:@selector(signin:) forControlEvents:UIControlEventTouchUpInside];
    [_signup addTarget:self action:@selector(clicksignup:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)clicksignup:(id)sender{
    signup * Signup = [[signup alloc]init];
    [self.navigationController pushViewController:Signup animated:YES];
}

-(void)signin:(id)sender{
    NSString * Username = _username.text;
    NSString * Password = _password.text;
    
    
    NSDictionary * parameters = @{
        @"name":Username,
        @"password":Password
    };
    NSString * url2 = @"http://106.52.91.37:8081/api/user/login";
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager POST:url2 parameters:parameters  headers:nil  progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary * returnjson = (NSDictionary *)responseObject;
        NSString * checklogin = returnjson[@"Type"];
        
      if([checklogin isEqualToString:@"success"] == 1){
            UIAlertController * alert1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"登陆成功" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert1 animated:YES completion:nil];
            AppDelegate *app = [UIApplication sharedApplication].delegate;
            UITabBarController * tabbar = [[UITabBarController alloc ]init];
        
            [[UITabBarItem appearance] setTitleTextAttributes:@{ UITextAttributeTextColor : [UIColor blackColor],UITextAttributeFont:[UIFont fontWithName:@"Marion-Italic" size:14.0]}  forState:UIControlStateNormal];
        
            testtable * view1 = [[testtable alloc]init];
            view1.title = @"发现";
            UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:view1];
            [tabbar addChildViewController:nav1];
            UIImage *mypage = [[UIImage imageNamed:@"tiktok"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            UIImage *mypage2 = [[UIImage imageNamed:@"tiktok2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            view1.tabBarItem.image = mypage;
            view1.tabBarItem.selectedImage = mypage2;
          
            releaseViewController * testview1 = [[releaseViewController alloc]init];
            testview1.title = @"发布广场";
           UIImage *playground1 = [[UIImage imageNamed:@"post"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
           UIImage *playground2 = [[UIImage imageNamed:@"post2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            UINavigationController * nav2 =[[UINavigationController alloc] initWithRootViewController:testview1];
        testview1.tabBarItem.image = playground1;
        testview1.tabBarItem.selectedImage = playground2;
            [tabbar addChildViewController:nav2];
          
            setting * testview2 = [[setting alloc]init];
            testview2.title = @"我的收藏";
            UINavigationController * nav3 =[[UINavigationController alloc] initWithRootViewController:testview2];
            [tabbar addChildViewController:nav3];
            UIImage *set1 = [[UIImage imageNamed:@"mycollect"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            UIImage *set2 = [[UIImage imageNamed:@"mycollect2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            testview2.tabBarItem.image = set1;
            testview2.tabBarItem.selectedImage = set2;
            app.window.rootViewController = tabbar;
        
            [app.window makeKeyAndVisible];
       }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"false");
    }];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
